/******************************************************************************
*          
This program uses the SPI ports on the JM60 chip.  It reads a specified amount of memory from
a Macronix 4Mbyte flash memory chip through SPIport1 and sends the same data out SPIport2.
It uses the push button on the JM60 to go from one section to another.

Code layout:

ininite loop

- push a button and read the chip id to verify that communication is taking place.
- push a button and read a seection of memory
    specfy size and address.  Stored in gau8Minicom array
- push a buttonb and write the data over SPI port2

  
****************************************************************************
Functions
  - hw_init();       MCU Initialization for speed, power and system settings
  - SPI_Init();      SPI Module Init for SPI port 1.  Connected to flash chip
  - SPI_Init2();      SPI Module Init for SPI port2.
  - SPI read byte();     Returns the last byte sent over the SPI port 
  - SPI write byte();   Writes a byte over the SPI port  
  - SPI read byte2();     Returns the last byte sent over the SPI port 
  - SPI write byte2();   Writes a byte over the SPI port  
Variables 
   UINT8 gau8Minicom[512];    array in which data is stored.  This chip has 4092 in RAM so this could be bigger
   UINT8   *pu8DataPointer;   used to store the location of gau8Minicom
   UINT16  u16Counter;        used as a counter
  UINT16  byte_count;         used to specify the amount of memomry read
  char    add1=0,add2=0,add3=0; specify address to read from  
                                                                          */ 
/*****************************************************************************/
/*Header connector
       
       oo
       oo
miso1  oo  mosi1
clk1   oo  SS1   >>chip to the right                                     
       oo
       oo                                         
       oo
3.3V   oo  gnd
       
      Chip
 
CS  1  8 VCC
SO  2  7 NC                                                
WP  3  6 CLK
gnd 4  5 SI


Some commands

c7  clear all
03  00 00 00  << three bytes specifying address    read memory
06  write enable
02  00 00 00  << three bytes specifying address  then followed by data to write 
9f  read identifier   C2 5E 16
05 read status register
20  00 00 00 sector  erase, specify sector  0-1023
d8  00 00 00 block erase , specify block    0-63


Blocks 0-63   ex block 23 = 23 x 2^16 - 24 x 2^16 - 1
Each block 16 sectors, 2^12 bytes big 

sectors 0-1023    each one 4096 bytes    
ex sector 39  addresses are 39x4096 to 40x4096-1  or in hex  0x27*0x1000 to 0x28*0x1000

4meg of memory runs from 0 to 0x40 00 00   */        

/* Include Files */
#include <hidef.h>
#include <stdio.h>          
#include "SD.h"

/**************Function prototypes****************/



 /************* Global Variables *****************/
UINT8 gau8Minicom[512];
extern UINT8 gu8IntFlags;
char numbuff[16];
/************************************************************************************************/
void main(void)
{
   UINT8   *pu8DataPointer;
  UINT16  u16Counter;
  UINT16  byte_count;
  char    add1=0,add2=0,add3=0;
  
/**************    Initializations  ***********************/  
  PTBPE_PTBPE5 = 1;                     // DEMOFLEXISJMSD board 
  PTBDD_PTBDD5 = _IN;                     // Initialize Button in 
  hw_init();                            /* MCU Initialization */
  SPI_Init();                           /* SPI Module Init */
  SPI_Init2();                           /* SPI Module Init */
 

  while(1)
  {
 
/***********************************************************************/
/************* Push button to complete this task: Read chip id    ****/   
    
        
       while(PTBD_PTBD5);                    /* Wait until button click */
   
       pu8DataPointer=gau8Minicom;
       SPI_SS=ENABLE;


	     SPI_Send_byte(0x9f);   //read chip id instruction
	     
       for(u16Counter=0;u16Counter<8;u16Counter++){
    	
	     *pu8DataPointer=SPI_Receive_byte();
	     pu8DataPointer++;
       }
    
    SPI_SS=DISABLE;
/************************************************************************/
/***** Read a block of memory and store in gua8minicom array*************/   
    
   while(PTBD_PTBD5);                    /* Wait until button click */
   
    add1=0;             //Specify the address 0-0x40 000 add3/add2/add1
    add2=0;
    add3=0;
    
    byte_count =  512;      //  Specify the number of bytes you want to read
    
    pu8DataPointer=gau8Minicom;
    
    SPI_SS=ENABLE;


	     SPI_Send_byte(0x03);   //read chip id instruction
	     SPI_Send_byte(add1);
	     SPI_Send_byte(add2);
	     SPI_Send_byte(add3);
	     
       for(u16Counter=0;u16Counter<byte_count;u16Counter++){
    	
	     *pu8DataPointer=SPI_Receive_byte();
	     pu8DataPointer++;
       }
    
    SPI_SS=DISABLE;

/************************************************************************/
/************* Send the array out over the 2nd SPI port *****************/   
    
   while(PTBD_PTBD5);                    /* Wait until button click */
   
        
    pu8DataPointer=gau8Minicom;
    
    SPI_SS2=ENABLE;


	     for(u16Counter=0;u16Counter<byte_count;u16Counter++){
    	
	     SPI_Send_byte2(*pu8DataPointer); 
	     pu8DataPointer++;
       }
    
    SPI_SS2=DISABLE;

  }
}


